﻿using LumiSoft.Net.POP3.Client;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;
using System.Text;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO:1、缺少IMAP协议的邮件读取
    ///      2、缺少各种邮件匹配机制
    /// </summary>
    public class ReceiveEmailNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            ReceiveEmailNParam? param = item.ControlParam as ReceiveEmailNParam;
            //暂时先只写阳光邮箱的读取。其他邮箱的暂时不做。没有时间。
            POP3EmailUtil emailUtil;
            bool ssl = param.Ssl ?? false;
            switch(param.MailType){ 
                case EmailType.SUN:
                    int port = int.Parse(SystemUtil.GetAPPConfig("SunGetPortPOP3"));
                    if(ssl){
                        port = int.Parse(SystemUtil.GetAPPConfig("SunGetPortSllPOP3"));
                    }
                    emailUtil = new POP3EmailUtil(SystemUtil.GetAPPConfig("SunEmailServer"), port, ssl);
                    break;
                default:
                    throw new RpaRuntimeException("邮箱类型不正确");
                    break;
            }
            string account = param.Account;
            this.GetStringVar(ref account, globleData);
            string password = param.Password;
            this.GetStringVar(ref password, globleData);
            emailUtil.Login(account, password);
            //由于POP3客户端无法标记已读，但是阳光邮箱暂时只有POP3邮箱服务。所以暂时社区已读等功能，未来换成imap服务再说
            //int? dirType = param.DirType;
            //string dirName = param.DirName;
            //int? mailNum = param.MailNum;
            //bool? onlyUnread = param.UnReadMail;
            //bool? tagRead = param.TagReaded;
            string titleFilter = param.TitleFilter;
            this.GetStringVar(ref titleFilter, globleData);
            string contentFilter = param.ContentFilter;
            this.GetStringVar(ref contentFilter, globleData);
            bool? saveAttach = param.SaveAttachFile;
            string attachSavePath = param.SavePath;
            this.GetStringVar(ref attachSavePath, globleData);
            string outVarName = param.MailListVar;
            EmailModel model = emailUtil.GetEmail(titleFilter, contentFilter);
            if(model == null){
                throw new RpaRuntimeException("未查询到对应邮件");
            }
            if(saveAttach ?? false) { 
                string attache = emailUtil.SaveAttach((POP3_ClientMessage)model.OriginalObj, attachSavePath);
                model.AttachPath = attache;
            }
            model.VarName = outVarName;
            this.saveVar(globleData, outVarName, model, DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
